package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.Mapper.ProductInfoMapper;
import com.example.demo.base.ArrayCountUtils;
import com.example.demo.base.Result;
import com.example.demo.entity.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.Mapper.AgriProductMapper;
import com.example.demo.base.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.example.demo.base.ArrayCountUtils.merge;

@RestController
@RequestMapping("/productInfo")
public class ProductInfoController {
    @Resource
    ProductInfoMapper productInfoMapper;//ProductInfo

    /**
     * 查询全部信息
     *
     * @return
     */

    @GetMapping("/all")
    public Result<?> findAll(@RequestParam String tenantId) {
        LambdaQueryWrapper<ProductInfo> wrapper = Wrappers.<ProductInfo>lambdaQuery();
        wrapper.eq(ProductInfo::getTenantId, Integer.valueOf(tenantId));
        return Result.success(productInfoMapper.selectList(wrapper));
    }

    @GetMapping("/chatData")
    public Result<?> chatData(@RequestParam String tenantId) {
        LambdaQueryWrapper<ProductInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductInfo::getTenantId, tenantId);
        List<ProductInfo> productInfoList = productInfoMapper.selectList(queryWrapper);
        List<ArrayCountUtils.BillsNums> billsNumsList = new ArrayList<>();
        for (ProductInfo info : productInfoList) {
            ArrayCountUtils.BillsNums billsNums = new ArrayCountUtils.BillsNums();
            billsNums.setId(info.getProName());
            billsNums.setSums(info.getWeight());
            billsNumsList.add(billsNums);
            billsNums = null;// 置空强制回收内存，以防数据量大内存泄露
        }
        List<ArrayCountUtils.BillsNums> res = merge(billsNumsList);
        return Result.success(res);
    }

    /**
     * 分页查询
     *
     * @param pageNum
     * @param pageSize
     * @return
     */

    @GetMapping(value = "/pageAll")
    public Result<?> findPageF(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "10") Integer pageSize,
                               @RequestParam String tenantId) {
        LambdaQueryWrapper<ProductInfo> wrapper = Wrappers.<ProductInfo>lambdaQuery();
        wrapper.eq(ProductInfo::getTenantId, Integer.valueOf(tenantId));
        Page<ProductInfo> Page = productInfoMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(Page);
    }


    /**
     * 用于投诉的时候根据商品溯源码获取加工厂租户id
     * @param id
     * @return
     */
    @GetMapping("/find")
    public Result<?> find(@RequestParam String id) {
        LambdaQueryWrapper<ProductInfo> wrapper = Wrappers.<ProductInfo>lambdaQuery();
        wrapper.eq(ProductInfo::getComId, id);
        if(productInfoMapper.selectList(wrapper).size()==0)
        {
            return Result.error(0, "未查到该商品溯源码");
        }

        return Result.success(productInfoMapper.selectList(wrapper).get(0).getTenantId());
    }
    /**
     * 根据主键查询
     *
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result<?> findByComId(@RequestParam String id,
                                 @RequestParam String tenantId) {
        ProductInfo productInfo = new ProductInfo();
        productInfo.setComId(Integer.valueOf(id));
        productInfo.setTenantId(Integer.valueOf(tenantId));
        ProductInfo tmp = productInfoMapper.selectByMultiId(productInfo);
        List<ProductInfo> productInfoList = new ArrayList<>();
        if (tmp != null) {
            productInfoList.add(tmp);
            return Result.success(productInfoList);
        }
        return Result.error(0, "没有查询到相关数据");
    }

    @GetMapping("/findOne")
    public Result<?> findByComId(@RequestParam String comId) {
        LambdaQueryWrapper<ProductInfo> wrapper = Wrappers.<ProductInfo>lambdaQuery();
        wrapper.eq(ProductInfo::getComId, comId);
        ProductInfo productInfo = productInfoMapper.selectOne(wrapper);
        if (productInfo != null) {
            return Result.success(productInfo);
        }
        return Result.error(0, "没有查询到相关产品出厂数据");
    }

    /**
     * 根据产品名称模糊查询
     *
     * @param name
     * @return
     */
    @GetMapping("/findByName")
    public Result<?> findByProductName(
            @RequestParam("name") String name,
            @RequestParam("tenantId") String tenantId) {
        LambdaQueryWrapper<ProductInfo> wrapper = Wrappers.<ProductInfo>lambdaQuery();
        wrapper.like(ProductInfo::getProName, name).eq(ProductInfo::getTenantId, tenantId);
        List<ProductInfo> productInfoList = productInfoMapper.selectList(wrapper);
        if (!productInfoList.isEmpty()) {
            return Result.success(productInfoList);
        }
        return Result.error(0, "没有查询到相关数据");
    }

    /*@GetMapping("/findByName/{name}")
    public Result<?> findByProductName(
                                       @PathVariable String name,
                                       @RequestParam String tenantId) {
        LambdaQueryWrapper<ProductInfo> wrapper = Wrappers.<ProductInfo>lambdaQuery();
        wrapper.like(ProductInfo::getProName, name)  .eq(ProductInfo::getTenantId,tenantId);;
        List<ProductInfo> productInfoList = productInfoMapper.selectList(wrapper);
        if (productInfoList.size() > 0)
            return Result.success(productInfoList);
        return Result.error(0, "没有查询到相关数据");
    }
*/

    /**
     * 根据code删除信息
     *
     * @return
     */
    @DeleteMapping("/delById")
    public Result<?> delete(@RequestParam String id,
                            @RequestParam String tenantId) {
        ProductInfo productInfo = new ProductInfo();
        productInfo.setComId(Integer.valueOf(id));
        productInfo.setTenantId(Integer.valueOf(tenantId));
        int res = productInfoMapper.deleteByMultiId(productInfo);
        if (res > 0)
            return Result.success();
        return Result.error(0, "删除失败");
    }

    /**
     * 添加消息
     *
     * @param comId
     * @param proName
     * @param weight
     * @param proBrith
     * @param expiration
     * @param note
     * @return
     */


    @GetMapping(value = "/add")
    public Result<?> saveP(@RequestParam String comId,
                           @RequestParam String proName,
                           @RequestParam String weight,
                           @RequestParam String proBrith,
                           @RequestParam String expiration,
                           @RequestParam String note,
                           @RequestParam String tenantId
    ) {
        if (comId.isEmpty() || proName.isEmpty() || weight.isEmpty() || proBrith.isEmpty() || expiration.isEmpty()) {
            return Result.error(0, "信息不能为空");
        }
        ProductInfo productInfo = new ProductInfo(Integer.valueOf(comId), proName, Double.valueOf(weight), proBrith, expiration, note, Integer.valueOf(tenantId));
        productInfoMapper.insert(productInfo);
        return Result.success();
    }

    /**
     * 根据主键修改信息
     *
     * @param comId
     * @param proName
     * @param weight
     * @param proBrith
     * @param expiration
     * @param note
     * @return
     */

    @GetMapping(value = "/update")
    public Result<?> updateById(@RequestParam String comId,
                                @RequestParam String proName,
                                @RequestParam String weight,
                                @RequestParam String proBrith,
                                @RequestParam String expiration,
                                @RequestParam String note,
                                @RequestParam String tenantId
    ) {
        if (comId.isEmpty() || proName.isEmpty() || weight.isEmpty() || proBrith.isEmpty() || expiration.isEmpty()) {
            return Result.error(0, "信息不能为空");
        }
        ProductInfo productInfo = new ProductInfo(Integer.valueOf(comId), proName, Double.valueOf(weight), proBrith, expiration, note, Integer.valueOf(tenantId));
        productInfoMapper.updateByMultiId(productInfo);
        return Result.success();
    }
}
